package com.mycard.ui.online;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.text.MessageFormat;

import android.support.v7.app.AppCompatActivity;
import com.ygo.feihua.R;

import java.text.*;
import android.widget.*;
import android.webkit.*;

//import com.tencent.smtt.sdk.*;

public class MyCardActivity extends AppCompatActivity implements MyCard.MyCardListener, NavigationView.OnNavigationItemSelectedListener {

    //private MyCardWebView mWebView;
	private WebView mWebView;
    private MyCard mMyCard;

	private String VERSION_NAME="3.1.5";

	private String APPLICATION_ID="cn.garymb.ygomobile";
	private int VERSION_CODE=310501;
  //  protected DrawerLayout mDrawerlayout;
  /*  private ImageView mHeadView;
    private TextView mNameView, mStatusView;
*/
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_mycard);
        //enableBackHome();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
          //  window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
         //   window.setStatusBarColor(getResources().getColor(R.color.mediumPurple));
        }

       // YGOStarter.onCreated(this);
        mMyCard = new MyCard(this);
        mWebView = (WebView) findViewById(R.id.mycard_web);
     //   mDrawerlayout = $(R.id.drawer_layout);

      //  NavigationView navigationView = $(R.id.nav_main);
      //  navigationView.setNavigationItemSelectedListener(this);
     //   View navHead = navigationView.getHeaderView(0);
      /*  mHeadView = (ImageView) navHead.findViewById(R.id.img_head);
        mNameView = (TextView) navHead.findViewById(R.id.tv_name);
        mStatusView = (TextView) navHead.findViewById(R.id.tv_dp);
       mWebView.enableHtml5();
*/
        WebSettings settings = mWebView.getSettings();
        settings.setUserAgentString(settings.getUserAgentString() + MessageFormat.format(
										" YGOMobile/{0} ({1} {2,number,#})",
										VERSION_NAME,
										APPLICATION_ID,
										VERSION_CODE
									));
		settings.setSaveFormData(true);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);

        // HTML5 configuration settings.
//        settings.setAppCacheMaxSize(Long.MAX_VALUE);
//        settings.setAppCachePath(context.getDir("appcache", Context.MODE_PRIVATE).getPath());
//        settings.setDatabasePath(context.getDir("databases", Context.MODE_PRIVATE).getPath());
//        settings.setGeolocationDatabasePath(context.getDir("geolocation", 0).getPath());

        settings.setJavaScriptCanOpenWindowsAutomatically(true);
       mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        mWebView.setHorizontalScrollBarEnabled(false);
      /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR_MR1) {
            GeolocationPermissions.getInstance();
        }*/
		settings.setJavaScriptEnabled(true);
        settings.setAllowContentAccess(true);
        settings.setAllowFileAccess(true);
		mWebView.setDownloadListener(new DownloadListener() {
				@Override
				public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
											long contentLength) {
					Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					try {
						startActivity(intent);
					} catch (Exception e) {
						Log.e("MyCardActivity","下载失败"+e);
						e.printStackTrace();
					}
				}
			});
		
		
		
									
//设置是否支持变焦
		/*mWebView.settings.setSupportZoom(true);
		//设置是否支持JavaScript
		mWebView.settings.setJavaScriptCanOpenWindowsAutomatically(true);
		mWebView.settings.setJavaScriptEnabled(true);
		mWebView.settings.setBuiltInZoomControls(true);//设置是否支持缩放
		mWebView.settings.setUseWideViewPort(true);
		mWebView.settings.setCacheMode(WebSettings.LOAD_DEFAULT);
		mWebView.settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
		mWebView.settings.setLoadWithOverviewMode(true);
		mWebView.settings.setAppCacheEnabled(true);
		mWebView.settings.setDomStorageEnabled(true);
		mWebView.requestFocus();
		*/
        /*mWebView.setUIClient(new XWalkUIClient(mWebView) {
				@Override
				public void onReceivedTitle(XWalkView view, String title) {
					super.onReceivedTitle(view, title);
					setTitle(title);
				}

				@Override
				public boolean onConsoleMessage(XWalkView view, String message, int lineNumber, String sourceId, ConsoleMessageType messageType) {
					if (BuildConfig.DEBUG)
						Log.i("webview", sourceId + ":" + lineNumber + "\n" + message);
					return super.onConsoleMessage(view, message, lineNumber, sourceId, messageType);
				}
			});*/
    //    mMyCard.attachWeb(mWebView, this);
        mWebView.loadUrl(mMyCard.getHomeUrl());
    }
	
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (mWebView != null) {
           // mWebView.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (mWebView != null) {
         //   mWebView.onNewIntent(intent);
        }
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }

    @Override
    protected void onResume() {
       // YGOStarter.onResumed(this);
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mWebView.stopLoading();
      //  mWebView.onDestroy();
        //YGOStarter.onDestroy(this);
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (doMenu(item.getItemId())) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private boolean doMenu(int id) {
        closeDrawer();
        switch (id) {
            case android.R.id.home:
                mWebView.loadUrl(mMyCard.getHomeUrl());
                break;
          /*  case R.id.action_deck_manager:
                startActivity(new Intent(this, DeckManagerActivity.class));
                closeDrawer();
                break;
            case R.id.action_arena:
                mWebView.loadUrl(mMyCard.getArenaUrl());
                break;
            case R.id.action_quit:
                finish();
                break;
            case R.id.action_home:
                onHome();
                break;
            case R.id.action_bbs:
                mWebView.loadUrl(mMyCard.getBBSUrl());
                break;*/
            default:
                return false;
        }
        return true;
    }

    @Override
    public void onLogin(String name, String icon, String statu) {
		Log.e("登录成功","登录成功"+name);
		Toast.makeText(this,"登录成功"+name,Toast.LENGTH_SHORT).show();
/*
        if(!TextUtils.isEmpty(icon)){
            Glide.with(this).load(Uri.parse(icon)).into(mHeadView);
        }
        mNameView.setText(name);
        mStatusView.setText(statu);*/
    }

    @Override
    public void onHome() {
        mWebView.loadUrl(mMyCard.getHomeUrl());
    }

    @Override
    public void watchReplay() {

    }

    @Override
    public void puzzleMode() {

    }

    @Override
    public void openDrawer() {
       /* if (!mDrawerlayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerlayout.openDrawer(Gravity.LEFT);
        }*/
    }

    @Override
    public void backHome() {
        finish();
    }

    @Override
    public void closeDrawer() {
      /*  if (mDrawerlayout.isDrawerOpen(Gravity.LEFT)) {
            mDrawerlayout.closeDrawer(Gravity.LEFT);
        }*/
    }

    @Override
    public void share(String text) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        intent.putExtra(Intent.EXTRA_TEXT, text);
        intent.putExtra(Intent.EXTRA_TITLE, getString(R.string.app_name));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, "请选择"));
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (doMenu(item.getItemId())) {
            return true;
        }
        return false;
    }
}

